#pragma once
#include "mysql.hpp"
#include "chat_session.hxx"
#include "chat_session-odb.hxx"

namespace xchat
{


class ChatSessionTable
{
public:

    using ptr = std::shared_ptr<ChatSessionTable>;

    ChatSessionTable(const std::shared_ptr<odb::core::database>& db) 
        : _db(db) {}

    //新增会话信息
    bool insert(ChatSession &cs)
    {
        try
        {
            //开启事务
            odb::transaction trans(_db->begin());
            _db->persist(cs);
            //提交事务
            trans.commit();
        }catch(std::exception &e){
            LOG_ERROR("新增cheat_session失败 {}-{} : {}", cs.chat_session_id(), cs.chat_session_name(), e.what());
            return false;
        }
        return true;
    }
    //删除会话信息
    bool remove(const std::string &ssid)
    {
        try
        {
            odb::transaction trans(_db->begin());
            typedef odb::query<ChatSession> query;
            typedef odb::result<ChatSession> result;
            //删除一个具体的、已知的对象（通常是通过主键或对象实例定位
            _db->erase_query<ChatSession>(query::chat_session_id == ssid); 
            typedef odb::query<ChatSessionMember> csm_query;
            typedef odb::result<ChatSessionMember> csm_result;
            _db->erase_query<ChatSessionMember>(csm_query::session_id == ssid); 
            trans.commit();
        }
        catch(std::exception &e){
            LOG_ERROR("删除chatSession失败或顺带删除chatSessionMember失败 {}: {}", ssid, e.what());
            return false;
        }
        return true;
    }
    bool remove(const std::string &uid, const std::string &pid)
    {
        //单聊会话的删除
        try
        {
            odb::transaction trans(_db->begin());
            typedef odb::query<SingleChatSession> Singlequery;
            typedef odb::result<SingleChatSession> Singleresult;
            auto res = _db->query_one<SingleChatSession>(Singlequery::css::chat_session_type == ChatSessionType::SINGLE
                && Singlequery::csm1::user_id == uid  
                && Singlequery::csm2::user_id == pid); 

            std::string cssid = res->_chat_session_id;
            
            typedef odb::query<ChatSession> query;
            typedef odb::result<ChatSession> result;
            //删除一个具体的、已知的对象（通常是通过主键或对象实例定位
            _db->erase_query<ChatSession>(query::chat_session_id == cssid); 
            typedef odb::query<ChatSessionMember> csm_query;
            typedef odb::result<ChatSessionMember> csm_result;
            _db->erase_query<ChatSessionMember>(csm_query::session_id == cssid); 
            trans.commit();
        }
        catch(std::exception &e){
            LOG_ERROR("查询SingleChatSessionID失败或删除ChatSessionID失败或顺带删除chatSessionMember失败 {}",  e.what());
            return false;
        }
        return true;
    }
    //通过会话ID 获取指定会话信息
    std::shared_ptr<ChatSession> select(const std::string &ssid)
    {
        std::shared_ptr<ChatSession> res;
        try
        {
            //开启事务
            odb::transaction trans(_db->begin());
            typedef odb::query<ChatSession> query;
            typedef odb::result<ChatSession> result;
            res = std::shared_ptr<ChatSession>(_db->query_one<ChatSession>(query::chat_session_id == ssid));
            //提交事务
            trans.commit();

        }catch(std::exception &e){
            LOG_ERROR("通过会话ID 获取指定会话信息失败 {}", ssid);
        }
        return res;
    }
    //获取指定用户单聊会话信息 SingleChatSession:_chat_session_id ; _friend_id
    std::vector<SingleChatSession> singleChatSession(const std::string &uid)
    {
        std::vector<SingleChatSession> res;
        try
        {
            //开启事务
            odb::transaction trans(_db->begin());
            typedef odb::query<SingleChatSession> query;
            typedef odb::result<SingleChatSession> result;
            result r(_db->query<SingleChatSession>(query::css::chat_session_type == ChatSessionType::SINGLE
                && query::csm1::user_id == uid  
                && query::csm2::user_id != query::csm1::user_id));
            for (const auto &it : r)
            {
                res.push_back(it);
            }
            //提交事务
            trans.commit();

        }catch(std::exception &e){
            LOG_ERROR("获取指定用户单聊会话信息失败{}", e.what());
        }
        return res;
    }
    //获取指定用户群聊会话信息 GroupChatSession: _chat_session_id _chat_session_name
    std::vector<GroupChatSession> groupChatSession(const std::string &uid)
    {
        std::vector<GroupChatSession> res;
        try
        {
            //开启事务
            odb::transaction trans(_db->begin());
            typedef odb::query<GroupChatSession> query;
            typedef odb::result<GroupChatSession> result;
            result r(_db->query<GroupChatSession>(query::css::chat_session_type == ChatSessionType::GROUP
                && query::csm::user_id == uid));
            for (const auto &it : r)
            {
                res.push_back(it);
            }
            //提交事务
            trans.commit();

        }catch(std::exception &e){
            LOG_ERROR("获取指定用户群聊会话信息失败 {}", e.what());
        }
        return res;
    }
private:
    std::shared_ptr<odb::core::database> _db;
};

}